package com.stn.zjx.givingtreasure.utils;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;

import com.stn.zjx.givingtreasure.constants.StnConstants;
import com.vondear.rxtools.RxSPTool;

import java.util.Locale;

/**
 * Created by Simon on 11/24/2017.
 */

public class LanguageUtils {

    public static final String CHINESE ="ZH";
    public static final String ENGLISH ="EN";
    public static final String DEFAULTS ="DEFAULT";

    public static void switchLanguage(Context context, String mode) {
        Resources resources = context.getResources();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        Configuration configuration = resources.getConfiguration();
        if (CHINESE.equals(mode)) {
            configuration.locale = Locale.CHINA;
        } else if (ENGLISH.equals(mode)) {
            configuration.locale = Locale.ENGLISH;
        } else if (DEFAULTS.equals(mode)) {
            configuration.locale = Locale.getDefault();
        } else {
            configuration.locale = Locale.getDefault();
        }

        resources.updateConfiguration(configuration, displayMetrics);
        RxSPTool.putString(context, StnConstants.LANGUAGE_SWITCH,mode);
    }

    public static String getLanguageMode(Context context) {

        String get_language = RxSPTool.getString(context, StnConstants.LANGUAGE_SWITCH);
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
       if(get_language==null||get_language.equals("")){
           RxSPTool.putString(context, StnConstants.LANGUAGE_SWITCH,CHINESE);
       }else{
           return get_language;
       }
        get_language= RxSPTool.getString(context, StnConstants.LANGUAGE_SWITCH);

       return get_language;
    }

}
